import { Component } from "@angular/core";
import { App,AlertController } from 'ionic-angular';

import { AppConfig } from '../../app/app.config';

import { RegistPage } from '../regist/regist';
import { ForgetPage } from './forget/forget';

import { HttpClient,HttpHeaders } from '@angular/common/http';
import { NativeStorage } from '@ionic-native/native-storage'; 
import { TranslateService } from '@ngx-translate/core';
import { SplashScreen } from '@ionic-native/splash-screen';
@Component({
    selector: 'page-login',
    templateUrl: 'login.html',
})
export class LoginPage {
    registPage = RegistPage
    forgetPage = ForgetPage
    firstpwd= 'password';
    iconfont = {
        'iconfont': true,
        'icon-login_open': this.firstpwd==='password',
        'icon-login_close': this.firstpwd==='text',
    }
    info = {
        account:'',
        password:''

    }
    constructor(
        private app:App,
        private http: HttpClient,
        private alertCtrl:AlertController,
        private storage:NativeStorage,
        private translate: TranslateService,
        private splashscreen:SplashScreen
    ) {
        translate.setDefaultLang('en');
    }
    ionViewDidEnter(){
      this.storage.getItem('lang').then(res=>{
          this.translate.use(res)
      })
    }
    alertb(title,subTitle,f=undefined){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: [{
                text:'OK',
                handler:()=>{
                    if( typeof f != 'undefined'){
                        f();
                    }
                }
            }]
        });
        alert.present();
    }
    goLogin(){
        this.http.post(AppConfig.getUrl()+'login/login', AppConfig.urlEcode(this.info), { headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( res => {
                if(res['code']===101){
                    this.storage.setItem('level',res['data']['info']['level'])
                    this.storage.setItem('token',res['data']['token']).then(()=>{
                        this.alertb('login success!','',()=>{
                            this.splashscreen.show();
                            window.location.reload();
                        });
                    }).catch((err)=>{
                        console.log(err)
                    });
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
    }
    goto(page:any){
        this.app.getRootNav().push(page);
    }
    toggle(){
        let iconfont = document.querySelector('#sign')
        if(this.firstpwd=='password'){
            this.firstpwd = 'text'
            iconfont.classList.remove('icon-login_open')
            iconfont.classList.add('icon-login_close')
        }else{
            this.firstpwd = 'password'
            iconfont.classList.remove('icon-login_close')
            iconfont.classList.add('icon-login_open')
        }
    }
}